package com.itheima.controller;


import com.itheima.common.JsonResult;
import com.itheima.common.SnowFlake;
import com.itheima.pojo.client.UpdateShoppingCartByDishIdDto;
import com.itheima.pojo.client.selectShoppingCartAllVo.SelectShoppingCartAllVo;
import com.itheima.pojo.client.addShoppingCart.AddShoppingCartDto;
import com.itheima.pojo.client.addShoppingCart.AddShoppingCartVo;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ChenJunJie
 * @date 2022/6/18 17:23
 * 购物车controller层
 */
@RestController
@RequestMapping(value = "/shoppingCart", produces = "application/json;charset=utf-8")
public class ShoppingCartController {

    @Autowired
    private SnowFlake snowFlake;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private ShoppingCartService shoppingCartService;

    //添加购物车
    @PostMapping("/add")
    public JsonResult<AddShoppingCartVo> addShoppingCart(@RequestBody AddShoppingCartDto shoppingCartDto, HttpServletRequest request) {

        //设置userID
        String token = request.getHeader("AuthorizationUser");
        String userId = redisTemplate.opsForValue().get(token);
        shoppingCartDto.setUserId(userId);

        AddShoppingCartVo addShoppingCartVo = shoppingCartService.addShoppingCart(shoppingCartDto);

        if (addShoppingCartVo!=null){
            return JsonResult.select(addShoppingCartVo,"添加成功");
        }else {
            return JsonResult.error("添加失败");
        }
    }

    //查询购物车列表通过userid
    @GetMapping("/list")
    public JsonResult<List<SelectShoppingCartAllVo>> selectShoppingCartAll(HttpServletRequest request){
        String token = request.getHeader("AuthorizationUser");
        String userId = redisTemplate.opsForValue().get(token);

        List<SelectShoppingCartAllVo> shoppingCar = shoppingCartService.selectShoppingCartAll(userId);

        return JsonResult.select(shoppingCar,"查询成功");

    }

    //通过userid删除购物车
    @DeleteMapping("/clean")
    public JsonResult<String> deleteShoppingCartByUserId(HttpServletRequest request){

        String token = request.getHeader("AuthorizationUser");
        String userId = redisTemplate.opsForValue().get(token);

        boolean b = shoppingCartService.deleteShoppingCartByUserId(userId);

        if (b){
            return JsonResult.success("清空购物车成功");
        }else {
            return JsonResult.error("暂无数据可以清空");
        }
    }

    //通过菜品id/套餐id与userid修改商品份数(减一操作)
    @PostMapping("/sub")
    public JsonResult<List<SelectShoppingCartAllVo>> updateShoppingCartByDishId(@RequestBody UpdateShoppingCartByDishIdDto id,HttpServletRequest request) {

        String token = request.getHeader("AuthorizationUser");
        String userId = redisTemplate.opsForValue().get(token);

        id.setUserId(userId);

        List<SelectShoppingCartAllVo> shoppingCart = shoppingCartService.updateShoppingCartByDishId(id);

        if (shoppingCart!=null){
            return JsonResult.select(shoppingCart, "删除成功");
        }else {
            return JsonResult.error("删除失败");
        }
    }
}
